<?php


namespace App\Http\Controllers\Api;
use App\Helpers\Api\MiniProgramTrait;
use App\Http\Controllers\BaseController;
use App\Jobs\OrderDeliverJob;
use App\Jobs\RandStarJob;
use App\Models\Article;
use App\Models\ArticleRelateCircle;
use App\Models\Circle;
use App\Models\Enum;
use App\Models\Subscribe;
use App\Models\User;
use App\Models\UserJoinCircle;
use App\Models\UserNotInterestTag;
use App\Models\UserRead;
use App\Mongo\CircleMon;
use App\Services\PublishTemplateService;
use Illuminate\Support\Facades\Redis;
use Intervention\Image\Facades\Image;
use itbdw\Ip\IpLocation;
use function Qiniu\base64_urlSafeDecode;


class TestController extends BaseController
{
    use MiniProgramTrait;


    public function index() {
//        $this->dispatch(new OrderDeliverJob(49));

//        $tomorrow = date("Y-m-d",strtotime("+1 day"));
//        $tomorrow_start = $tomorrow . " 00:00:01";
//        $tomorrow_end_at = $tomorrow . " 23:59:59";
//        $activity_list = Circle::where('is_activity', 1)->where('activity_end_at', '>=', $tomorrow_start)->where('activity_end_at', '<=', $tomorrow_end_at)->get();
//        if ($activity_list) {
//            foreach ($activity_list as $activity_item) {
//                Subscribe::where('type', Subscribe::TYPE_ACTIVITY_COUNT_DOWN)->withCertain('user', ['openid'])->chunkById(50, function ($list) use($activity_item) {
//                    foreach ($list as $item) {
//                        //check weather join or not
//                        $exists = UserJoinCircle::where('user_id', $item->user_id)->where('circle_id', $activity_item->id)->where('is_activity', 1)->exists();
//                        if ($exists) {
//                            $sql = 'SELECT id,user_id,activity_star_num,
//(SELECT count(DISTINCT activity_star_num) FROM articles AS b WHERE activity_id = '.$activity_item->id.' and deleted_at is null and a.activity_star_num<b.activity_star_num)+1 AS rank
//FROM articles AS a where activity_id = '.$activity_item->id.' and deleted_at is null ORDER BY rank';
//
//                            $res = \DB::select($sql);
//                            foreach ($res as $v) {
//                                if ($v->user_id == $item->user_id) {
//                                    $service = new PublishTemplateService();
//                                    $service->activity_countdown($item->user->openid, $activity_item->title, $activity_item->id, $v->rank);
//                                    $item->delete();
//                                    break;
//                                }
//                            }
//                        }
//                    }
//                });
//            }
//        }
//        exit;

//        exit;
//        $circle = Circle::where('id', 3106)->first();
//        $circle_title = $circle->title;
//        $service = new PublishTemplateService();
//        $service->circleArticleNotice('ojHxB5TULFUqriLM3BrKCJRiKCJg', $circle_title, 1, 1, $circle->id, $circle->is_activity);
//        exit;
//
//        $arr = [
//            'first' => '1101',
//            'second' => '1100,1098',
//            'third' => '1099',
//            'desc' => 'desc'
//        ];
//        echo json_encode_cn($arr);exit;
//        $fake_uid = '166987278952955904';
//        $content = UserRead::where('uid', $fake_uid)->value('content');
//        $read_article_ids = rtrim(ltrim($content, ','), ',');
//        var_dump($read_article_ids);
//        exit;
//        var_dump(IpLocation::getLocation('175.24.96.14'));exit;
//
//        $rand_minute = 1;//rand(30, 180);
//        RandStarJob::dispatch(580)->delay(now()->addMinutes($rand_minute));
//        return;
//        $user = User::find(361);
//        $user->is_black = 0;
//        $user->save();
//        exit;
//        $service = new PublishTemplateService();
//        $service->comment('ojHxB5T0IybCee8TC4t8_R0h4FTM', '测试', '无缺', 1, 2);
//
//        exit;
//        Enum::create([
//            'key' => 11,
//            'value' => 333
//        ]);
//        exit;
//        \Cache::flush();
//        $num = Enum::find(5);
//        $num->key = 'report';
//        $num->save();

//        return;
//        $arr_tag_id = UserNotInterestTag::where('user_id', 362)->get()->pluck('tag_id')->all();

//        return $arr_tag_id;
//        $aa = Circle::whereNotExists(function ($q) {
//            $q->select(\DB::raw(1))->from('circle_category_relates')->whereRaw('circles.id = circle_category_relates.circle_id');
//        })->get();
//        return $aa;
//
//        $aa = reverse_geocode('120.1117490', '30.3418330', true);
//        return $aa;
//
//        return;
//        $article = Article::find(34);
//        $arr_circle_id = [2277, 2273];
//        $new_arr = [];
//        foreach ($arr_circle_id as $val) {
//            $new_arr[$val] = [
//                'article_user_id' => $article->user_id
//            ];
//        }
//        $article->circles()->sync($new_arr);
//
//
//
//        return;
//        Redis::incr('user_latest_fans_count_1', 1);
//
//
//        $aa = Redis::mget(['user_latest_fans_count_1']);
//        return $aa;
//
//
//        $img = Image::make(public_path('test/1.jpg'));
//        $height = $img->height();
//        $width = $img->width();
//        $img->crop($width, $height-177)->save(public_path('test/1_.jpg'));;
//
//
//
//
//        exit;
        $app = $this->miniProgram();
        $list = $app->subscribe_message->getTemplates();
        return $list;
//        $aa->comment();exit;

        return CircleMon::find(1);
        $bars = CircleMon::where('location', 'near', [
            '$geometry' => [
                'type' => 'Point',
                'coordinates' => [
                    120.816828,27.897196
                ],
            ],
            '$maxDistance' => 100000,
        ])->where('city', '温州')->get();
        return $bars;

        exit;
        CircleMon::create([
            'id' => 5,
            'title' => '瑞安市',
            'location' => [
                'type' => 'Point',
                'coordinates' => [
                    120.651252,27.781691
                ]
            ],
            'city' => '温州'
        ]);
    }
}

